Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TensorRT EP] Fix InferenceSession::Run() not thread-safe issue #19301

Merged
merged 10 commits into from
Jan 30, 2024

Conversation

chilo-ms
Copy link
Contributor

@chilo-ms chilo-ms commented Jan 29, 2024

Given that InferenceSession::Run() is guaranteed to be thread-safe meaning multiple threads can call this function concurrently,
TRT EP needs to carefully take care of concurrency here, if not, following concurrent issue might happen:

  • It's suggested that to perform inference concurrently in multiple streams, use one trt execution context per stream.
    In the design of TRT EP (Not apply per-thread context implementation) and if multiple threads are calling InferenceSession::Run() concurrently, the trt execution context instance is shared by all the threads and each thread aquires different stream from ORT.
    So TRT EP will end up having one trt execution context using multiple streams which is not suggested.
    But, since the whole compute_func() is protected by the lock and if cudaStreamSynchronize() is enforced here, one trt execution context per stream is guaranteed.

Therefore, TRT EP needs to call cudaStreamSynchronize() at compute_func() which means to wait until stream has completed all operations to prevent the concurrent issue mentioned above

github isse: #19275

@chilo-ms chilo-ms requested a review from jywu-msft January 29, 2024 02:39
@chilo-ms chilo-ms marked this pull request as ready for review January 29, 2024 18:39
jywu-msft
jywu-msft previously approved these changes Jan 29, 2024
@chilo-ms chilo-ms merged commit 00d0481 into main Jan 30, 2024
95 of 97 checks passed
@chilo-ms chilo-ms deleted the chi/trt_ep_concurrent_fix branch January 30, 2024 01:36
YUNQIUGUO pushed a commit that referenced this pull request Jan 30, 2024
Given that InferenceSession::Run() is guaranteed to be thread-safe
meaning multiple threads can call this function concurrently,
TRT EP needs to carefully take care of concurrency here, if not,
following concurrent issue might happen:
    

- It's suggested that to perform inference concurrently in multiple
streams, use one trt execution context per stream.
In the design of TRT EP (Not apply per-thread context implementation)
and if multiple threads are calling InferenceSession::Run()
concurrently, the trt execution context instance is shared by all the
threads and each thread aquires different stream from ORT.
So TRT EP will end up having one trt execution context using multiple
streams which is not suggested.
But, since the whole compute_func() is protected by the lock and if
cudaStreamSynchronize() is enforced here, one trt execution context per
stream is guaranteed.
     
Therefore, TRT EP needs to call cudaStreamSynchronize() at
compute_func() which means to wait until stream has completed all
operations to prevent the concurrent

github isse: #19275
yf711 added a commit that referenced this pull request Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants